<template>
  <div>
    <el-form ref="formDateRef" label-width="120px" :model="formDate" :rules="formDateRules">
      <el-form-item label="部门名称" style="width:80%" prop="name">
        <el-input v-model="formDate.name" />
      </el-form-item>
      <el-form-item label="部门编码" style="width:80%" prop="code">
        <el-input v-model="formDate.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formDate.manager" placeholder="你选啊！" style="width:75%">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :value="item.id"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" style="width:80%" prop="introduce">
        <el-input
          v-model="formDate.introduce"
          type="textarea"
          :rows="3"
          placeholder="1-100字符"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button type="info" @click="doCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDepartments, getDates, getManagerList, updateDepartment } from '@/api/department'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门名称相同校验
    const isName = (rules, value, callback) => {
      let nameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      console.log(nameList)
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        const brother = this.originList.filter(item => item.pid === pid)
        nameList = brother.filter(item => item.id !== this.id).map(item => item.name)
      }
      if (nameList.includes(value)) {
        callback(new Error(`部门名称${value}已存在`))
      } else {
        console.log(1111)
        callback()
      }
    }
    const isCode = (rules, value, callback) => {
      let codeList = this.originList.filter(item => item.pid !== this.id).map(item => item.code)
      console.log(codeList)
      if (this.isEdit) {
        codeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (codeList.includes(value)) {
        callback(new Error(`部门编码${value}已存在`))
      } else {
        callback()
      }
    }
    return {
      formDate: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      formDateRules: {
        name: [
          { required: true, message: '部门名称不得为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称为1-50个字符', trigger: 'blur' },
          { validator: isName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不得为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码为1-50个字符', trigger: 'blur' },
          { validator: isCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不得为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不得为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍为1-100个字符', trigger: 'blur' }
        ]
      },
      managerList: []
    }
  },
  created() {
    this.getManagers()
    if (this.isEdit) {
      this.getDate()
    }
  },
  methods: {
    // 获取负责人列表
    async getManagers() {
      const { data } = await getManagerList()
      console.log(data)
      this.managerList = data.data
    },
    // 新增部门
    async doAdd() {
      const { data } = await addDepartments({ ...this.formDate, pid: this.id })
      console.log(data)
      this.$message.success(data.message)
      this.$emit('success')
    },
    // 编辑部门
    async editDepartment() {
      const { data } = await updateDepartment(this.formDate)
      console.log(data)
      this.$message.success(data.message)
      this.$emit('success')
    },
    // 获取数据（数据回显）
    async getDate() {
      const { data } = await getDates(this.id)
      console.log(data)
      this.formDate = data.data
    },
    // 判断是添加还是编辑
    submit() {
      this.isEdit ? this.editDepartment() : this.doAdd()
    },
    // 清空表单
    resetForm() {
      this.$refs.formDateRef.resetFields()
    },
    // 取消
    doCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
